473,463 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Append to PATH Environment Variable

I have an application that uses PInvoke to call into native DLLs. My
DLLimport attribute only has the name of the DLL that I need to import (i.e.
I leave it up to the OS to search the PATH for the DLL.) This all works
great when the user's PATH variable is setup correctly.

Unfortunately, there are times when the user's PATH is too full or whatever
to have the correct search path for my application.

What I was wondering is if there is a way to set the PATH variable to have
the correct search directory after my application starts up (yet before any
call to DLLImport). This way I can guarantee that the search is correct for
the running instace of my application.

I tried this at the beginning of my application to no avail:

System.Diagnostics.ProcessStartInfo info =
System.Diagnostics.Process.GetCurrentProcess().Sta rtInfo;

string origpath = info.EnvironmentVariables["path"];
string newpath = "C:\\mypath;" + origpath;
info.EnvironmentVariables["path"] = newpath;

****

I know I could create a separate executable that sets the Environment
variables and calls StartProcess to launch my application. I was hoping that
my application could set this path infromation without requiring another
executable to launch my application.

Thanks!
Nov 16 '05 #1
1 9640
Chris,

You could always call the SetEnvironmentVariable function through the
P/Invoke layer.

Also, if you can wait for .NET 2.0, you are able to take a function
pointer and assign it to a delegate to call. This will allow you to load
the dll (LoadLibrary, LoadLibraryEx), get the procedure address
(GetProcAddress), and then get a delegate that you can call which will call
the function at that address.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris B" <Ch****@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
I have an application that uses PInvoke to call into native DLLs. My
DLLimport attribute only has the name of the DLL that I need to import
(i.e.
I leave it up to the OS to search the PATH for the DLL.) This all works
great when the user's PATH variable is setup correctly.

Unfortunately, there are times when the user's PATH is too full or
whatever
to have the correct search path for my application.

What I was wondering is if there is a way to set the PATH variable to have
the correct search directory after my application starts up (yet before
any
call to DLLImport). This way I can guarantee that the search is correct
for
the running instace of my application.

I tried this at the beginning of my application to no avail:

System.Diagnostics.ProcessStartInfo info =
System.Diagnostics.Process.GetCurrentProcess().Sta rtInfo;

string origpath = info.EnvironmentVariables["path"];
string newpath = "C:\\mypath;" + origpath;
info.EnvironmentVariables["path"] = newpath;

****

I know I could create a separate executable that sets the Environment
variables and calls StartProcess to launch my application. I was hoping
that
my application could set this path infromation without requiring another
executable to launch my application.

Thanks!

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jeff Wagner | last post by:
Is it possible to append a new Path to some file permanently? It seems like a sys.path.append('/my/new/path') statement is temporary. In other words, where and in what file on a Win32 box or...
1
by: jwpioneer | last post by:
I have a need within an application to modify the path environment variable, as I need to find specific directories and remove them. I use the following code to do this: RegistryKey rkey = ...
1
by: Bonj | last post by:
Hi My application installs a front-end GUI, which runs code when buttons are clicked. It also installs a command-line utility, that is a console application. They are both installed to the...
6
by: Garfield | last post by:
We have written a small function to append a couple of paths to the path environmental variable. The code is: string PathValue = ""; string sAdd = ""; sAdd = ";" + <path1> + "Bin;" +...
10
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python:2.4 Based on a search of other posts in this group, it appears as though os.environ is one way to obtain the PATH environment variable. My questions: 1) is it...
3
by: HMS Surprise | last post by:
Is sys.path setup differnently in jython vs python? I have environment variables pythonpath and jythonpath set to include C:\python22 but the initial printout indicates it is being ignored. Also...
3
by: Wolfgang Meister | last post by:
I would like to append text to a file which might exist or not. What is the easiest way to open or create it depending if it exists or not and to set it into an "append" text mode?
0
by: cnathaide | last post by:
Hi, I have written a simple command line application in C# and created the accompanying setup project in VS 2005. When the user installs the application on the his/her machine, I would like...
2
by: Blair | last post by:
Hello, I have a old enviromantal path in LIB file that, every time I compile gives a error message. It does not stop the compiler just gives a warning. This has become very annoying. I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.